package com.heima.user.interceptor;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @date 2024/8/21 17:42
 * @Author JinCc
 * @Description TODO
 * @Version 1.0
 */
@Slf4j
public class FollowInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userId = request.getHeader("userId");
        log.info("获取到用户ID：{}",userId);
        if(StringUtils.isNotBlank(userId)){
            //放到当前上下文（线程）
            WmUser user = new WmUser();
            user.setId(Integer.parseInt(userId));
            WmThreadLocalUtil.setUser(user);
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        WmThreadLocalUtil.remove();
    }
}
